package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.ui.dialog.SyDialogHelper;
import com.typy.www.tpay.ui.dialog.SyMessageDialog;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.ClickFastUtil;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.PwdDailogUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.typy.www.tpay.util.inter.InputCallBack;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/7/13.
 */

public class BuyDetailActivity extends BaseBackActivity {
    @BindView(R.id.tv_xiane)
    TextView tvXiane;
    @BindView(R.id.tv_buytitle)
    TextView tvBuytitle;
    @BindView(R.id.et_chushoushuliang)
    AutoCompleteTextView etChushoushuliang;
    @BindView(R.id.ll_xiane)
    PercentLinearLayout llXiane;
    @BindView(R.id.et_chushoujiage)
    AutoCompleteTextView etChushoujiage;
    @BindView(R.id.ll_danjia)
    PercentLinearLayout llDanjia;
    @BindView(R.id.et_chushouleixing)
    AutoCompleteTextView etChushouleixing;
    @BindView(R.id.ll_chushouleixing)
    PercentLinearLayout llChushouleixing;
    @BindView(R.id.et_huodejine)
    AutoCompleteTextView etHuodejine;
    @BindView(R.id.ll_huodejine)
    PercentLinearLayout llHuodejine;
    @BindView(R.id.tv_quxiao)
    TextView tvQuxiao;
    @BindView(R.id.tv_queding)
    TextView tvQueding;
    @BindView(R.id.ll_sell)
    PercentLinearLayout llSell;
    @BindView(R.id.tv_chushoushuliang)
    TextView tvChushoushuliang;
    @BindView(R.id.tv_chushoujiage)
    TextView tvChushoujiage;
    @BindView(R.id.tv_chushouleixing)
    TextView tvChushouleixing;
    @BindView(R.id.tv_huodejine)
    TextView tvHuodejine;
    private String name;
    private String xiane;
    private String shuliang;
    private String danjia;
    private String jine;
    private String typeName;
    private int classType;
    private String typeId;
    private String fdId;

    public static void show(Context context, int classType, String name, String xiane, String shuliang, String danjia, String jine, String typeName, String typeId, String fdId) {
        Intent intent = new Intent();
        intent.putExtra("name", name);
        intent.putExtra("xiane", xiane);
        intent.putExtra("shuliang", shuliang);
        intent.putExtra("danjia", danjia);
        intent.putExtra("jine", jine);
        intent.putExtra("typeName", typeName);
        intent.putExtra("classType", classType);
        intent.putExtra("typeId", typeId);
        intent.putExtra("fdId", fdId);
        IntentUtil.intent2Activity(context, intent, BuyDetailActivity.class);
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_buydetail;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        classType = getIntent().getIntExtra("classType", 0);
        name = getIntent().getStringExtra("name");
        xiane = getIntent().getStringExtra("xiane");
        shuliang = getIntent().getStringExtra("shuliang");
        danjia = getIntent().getStringExtra("danjia");
        jine = getIntent().getStringExtra("jine");
        typeName = getIntent().getStringExtra("typeName");
        typeId = getIntent().getStringExtra("typeId");
        fdId = getIntent().getStringExtra("fdId");
        updateTitle();
    }

    private void updateTitle() {
        Logutil.print(classType);
        if (classType == ClassHoldUtil.BUYFROM) {
            setTitle("向" + name + "购买");
            UpdataUiUtil.getDefaul().setText(xiane + " 个", tvXiane)
                    .setText("您确定要向[" + name + "]购买", tvBuytitle)
                    .setText(shuliang, etChushoushuliang)
                    .setText(danjia, etChushoujiage)
                    .setText(jine, etHuodejine)
                    .setText(typeName, etChushouleixing)
                    .setTextId(R.string.goumaishuliang, tvChushoushuliang)
                    .setTextId(R.string.goumaijiage, tvChushoujiage)
                    .setTextId(R.string.goumaileixing, tvChushouleixing)
                    .setTextId(R.string.goumaijine, tvHuodejine);
        } else {
            setTitle("向" + name + "出售");
            UpdataUiUtil.getDefaul().setText(xiane + " 个", tvXiane)
                    .setText("您确定要向[" + name + "]出售", tvBuytitle)
                    .setText(shuliang, etChushoushuliang)
                    .setText(danjia, etChushoujiage)
                    .setText(jine, etHuodejine)
                    .setText(typeName, etChushouleixing).
                    setTextId(R.string.chushoushuliang, tvChushoushuliang)
                    .setTextId(R.string.chushoujiage, tvChushoujiage)
                    .setTextId(R.string.chushouleixing, tvChushouleixing)
                    .setTextId(R.string.huodejine, tvHuodejine);
        }
    }


    @OnClick({R.id.tv_quxiao, R.id.tv_queding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_quxiao:
                finish();
                break;
            case R.id.tv_queding:
                if (ClickFastUtil.isFastClick1000())
                    PwdDailogUtil.show(this, new InputCallBack() {
                        @Override
                        public void onInputFinish(String result) {
                            savebuyorder(result);
                        }
                    });
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private void savebuyorder(String password) {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdId", fdId);
        paramsUtil.put("fdQuantity", shuliang);
        paramsUtil.putMd5("fdPayPwd", password);
        String url = "";
        if (classType == ClassHoldUtil.BUYFROM)
            url = Constance.getFundbuyURL();
        else
            url = Constance.getSellerURL();
        HTTPUtils.post(this, url, paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", "提交成功", "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {
                        EventBus.getDefault().post(new MessageEvent(MessageAction.FEFRESHDITAIL));
                        RecordActivity.show(getActivity(), ClassHoldUtil.DigiDetail, typeName);
                        getActivity().finish();
                        ActivicationActivity.instance.finish();
                    }
                });

            }

            @Override
            public void onResponseErr(int code, String msg) {
                PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", msg, "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {
//
                    }
                });
            }
        });
    }
}
